package com.example.myapp;

import static android.content.Context.MODE_PRIVATE;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class MineFragment extends Fragment {

    public MineFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 加载布局文件
        return inflater.inflate(R.layout.mine, container, false);
    }

    @SuppressLint("WrongViewCast")
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 使用 view.findViewById 来查找控件
        Button exit_login = view.findViewById(R.id.btn_exit_login);
        exit_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击退出登录按钮
                // 将登录的状态保存到本地
                // 使用 SharedPreferences 保存登录状态
                SharedPreferences sharedPreferences = getActivity().getSharedPreferences("login_status", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("login", false);
                editor.apply(); // 提交修改

                Toast.makeText(getActivity(), "退出登录", Toast.LENGTH_SHORT).show();

                // 跳转到登录页面
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);

                // 关闭当前页面
                getActivity().finish();

            }
        });
    }
}